//
//  MyAdTools.m
//  TeaDao
//
//  Created by HappyMilk 01 on 2017/4/5.
//  Copyright © 2017年 Teach. All rights reserved.
//

#import "MyAdTools.h"
#import "LoginHeader.h"
#import "HHWebViewController.h"

@interface MyAdTools()

@property(nonatomic, strong) GADInterstitial *interstitial;
@property(nonatomic,copy) NSString *cyID;
@end


@implementation MyAdTools

+ (MyAdTools *)shareInstance
{
    static MyAdTools * tool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [[MyAdTools alloc] init];
    });
    return tool;
}

-(void)initWithCYID:(NSString *)cyID{
    _cyID = cyID;
    self.interstitial = [self createAndLoadInterstitial];
}


- (GADInterstitial *)createAndLoadInterstitial {
    
    GADInterstitial *interstitial = [[GADInterstitial alloc] initWithAdUnitID:_cyID];
    interstitial.delegate = self;

    GADRequest *request =[GADRequest request];
    [interstitial loadRequest:request];
    return interstitial;
}

- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial {
    self.interstitial = [self createAndLoadInterstitial];
}

-(BOOL)showInterstitialWithVC:(UIViewController *)vc{
    if(_interstitial ==nil)
        return false;
    
    if([MPWeChat stander].m_adStatus < 0)
        return false;
    
    if([MPWeChat stander].m_adStatus == 1 && [MPWeChat stander].m_isLogin == false)
        return false;
    
    if ([self.interstitial isReady] && IS_CHINESE) {
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.interstitial presentFromRootViewController:vc];
        });
        
        return YES;
    }
    return false;
}


-(UIButton *)addBannerADView2VC:(UIViewController *)vc atPositionY:(CGFloat)y{
    if([MPWeChat stander].m_adLink.length<5)
        return nil;
    
    
    CGFloat height =  [UIScreen mainScreen].bounds.size.height>720? 90.0:50.0;
    if( [UIScreen mainScreen].bounds.size.height<400)
        height=32;
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, y, [UIScreen mainScreen].bounds.size.width, height)];
    [vc.view addSubview:btn];
    [btn addTarget:self action:@selector(onADBannerClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    btn.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[MPWeChat stander].m_adCover]];
        UIImage *image = [[UIImage alloc]initWithData:data];
        dispatch_sync(dispatch_get_main_queue(), ^{
            [btn setImage:image forState:0];
        });
        
    });
    
    return btn;
}

-(void)onADBannerClicked:(UIButton *)btn{
    id target = btn;
    while (target) {
        target = ((UIResponder *)target).nextResponder;
        
        if ([target isKindOfClass:[UIViewController class]]) {
            UIViewController *vc = (UIViewController *)target;
            HHWebViewController *webVC = [[HHWebViewController alloc]init];
            //            webVC.url = @"https://itunes.apple.com/cn/app/id989673964";
            webVC.url = [MPWeChat stander].m_adLink;
            
            [vc presentViewController:webVC animated:true completion:nil];
            break;
        }
    }
}


@end
